package com.uicole.push.server.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.uicole.push.server.model.DeviceInfo;
import com.uicole.push.server.service.impl.DeviceInfoServiceImpl;

@Controller
public class DeviceInfoController {

	@Autowired
	private DeviceInfoServiceImpl deviceInfoService;
	// DeviceInfos
	@RequestMapping(value = "/deviceInfos", method = RequestMethod.GET)
	public String deviceInfosList(Model model) {
		model.addAttribute("deviceInfos", deviceInfoService.getAll());
		return "deviceInfo/deviceInfos_index";
	}

	@RequestMapping(value = "/deviceInfos/new", method = RequestMethod.GET)
	public String deviceInfosNew(Model model) {
		model.addAttribute("deviceInfo", new DeviceInfo());
		return "deviceInfo/deviceInfos_create";
	}

	@RequestMapping(value = "/deviceInfos", method = RequestMethod.POST)
	public String deviceInfosCreate(Model model, DeviceInfo deviceInfo) {
		DeviceInfo existing = deviceInfoService.get(deviceInfo.getId());
		if (existing != null) {
			model.addAttribute("error", "DeviceInfo already exist !");
			model.addAttribute("deviceInfos", deviceInfoService.getAll());
			return "deviceInfo/deviceInfos_index";
		}
		deviceInfoService.save(deviceInfo);
		model.addAttribute("info", "DeviceInfo created successfully");
		return "deviceInfo/deviceInfos_get";
	}

	@RequestMapping(value = "/deviceInfos/{id}", method = RequestMethod.GET)
	public String deviceInfosRetrieve(@PathVariable Long id, Model model) {
		DeviceInfo deviceInfo = deviceInfoService.get(id);
		model.addAttribute("deviceInfo", deviceInfo);
		return "deviceInfo/deviceInfos_get";
	}

	@RequestMapping(value = "/deviceInfos/edit/{id}", method = RequestMethod.GET)
	public String deviceInfosEdit(@PathVariable Long id, Model model) {
		DeviceInfo deviceInfo = deviceInfoService.get(id);
		model.addAttribute("deviceInfo", deviceInfo);
		return "deviceInfo/deviceInfos_update";
	}

	@RequestMapping(value = "/deviceInfos/update", method = RequestMethod.POST)
	public String deviceInfosUpdate(Model model, DeviceInfo deviceInfo) {
		DeviceInfo oldDeviceInfo = deviceInfoService.get(deviceInfo.getId());
		
		model.addAttribute("info", "DeviceInfo updated successfully");
		model.addAttribute("deviceInfo", oldDeviceInfo);
		return "deviceInfo/deviceInfos_get";
	}

	@RequestMapping(value = "/deviceInfos/delete/{id}", method = { RequestMethod.GET })
	public String deviceInfosDelete(@PathVariable Long id, Model model) {
		deviceInfoService.remove(id);
		model.addAttribute("info", "DeviceInfo deleted successfully");
		model.addAttribute("deviceInfos", deviceInfoService.getAll());
		return "deviceInfo/deviceInfos_index";
	}

	@RequestMapping(value = "/deviceInfos/search", method = RequestMethod.POST)
	public String deviceInfosSearch(Model model, DeviceInfo deviceInfo) {
		List<DeviceInfo> deviceInfos = deviceInfoService.getAll();
		model.addAttribute("deviceInfos", deviceInfos);
		model.addAttribute("search", true);
		return "deviceInfo/deviceInfos_index";
	}

	public DeviceInfoServiceImpl getDeviceInfoService() {
		return deviceInfoService;
	}

	public void setDeviceInfoService(DeviceInfoServiceImpl deviceInfoService) {
		this.deviceInfoService = deviceInfoService;
	}

}
